package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorsMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;

import java.beans.Transient;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class DishServiceimpl  implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorsMapper dishFlavorsMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    /**
     * 添加菜品和对应口味
     * @param dishDTO
     */
    @Override
    @Transactional
    public void savewithFlavor(DishDTO dishDTO) {


        Dish dish = new Dish();
        //属性拷贝
        BeanUtils.copyProperties(dishDTO,dish);
        //向菜品表插入一条数据
        dishMapper.insert(dish);
        //获取insert主键值
        Long dishId = dish.getId();
        //向口味表插入多条数据
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null && flavors.size()> 0){
            /**
             * 批量循环为dish_id 赋值
             */
            for (DishFlavor dishFlavor1 : flavors){
                dishFlavor1.setDishId(dishId);
            }
            dishFlavorsMapper.insertBatch(flavors);
        }
    }

    /**
     *  菜品分页查询
     * @param dishPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
        Page<DishVO> page = dishMapper.dishQuery(dishPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 批量删除菜品
     * @param ids
     */
    @Override
    @Transactional
    public void deleteByid(List<Long> ids) {
        //判断菜品状态是否为起售 如果为起售则不能删除
        for (Long id : ids) {
            Dish dish = dishMapper.getStagtusById(id);
            if (dish.getStatus() == StatusConstant.ENABLE){
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }
        //判断菜品是否关联套餐，如果在套餐内不能删除
           List<Long> setmealIds = setmealDishMapper.getSetmealIdBydishId(ids);
           if (setmealIds != null && setmealIds.size()>0){
               throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
           }

        //删除菜品
        for (Long id : ids) {
            dishMapper.deleteById(id);
            //删除菜品口味
            dishFlavorsMapper.deletById(id);
        }


    }

    /**
     * 根据Id查询菜品及口味信息
     * @param id
     * @return
     */
    @Override
    public DishVO getByIdwhithFlavors(Long id) {
        //查询菜品信息
        Dish dish = dishMapper.getStagtusById(id);
        //查询口味信息
        List<DishFlavor> dishFlavors = dishMapper.getFlavorsById(id);
        //将查询到的信息封装到VO对象中
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish,dishVO);
        dishVO.setFlavors(dishFlavors);
        return dishVO;
    }

    /**
     * 修改菜品
     * @param dishDTO
     */
    @Override
    public void updatewithFlavors(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        //修改菜品信息
        dishMapper.update(dish);
        //删除原有口味信息
        dishFlavorsMapper.deletById(dish.getId());
        //重新插入口味信息
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null && !flavors.isEmpty()){

            for (DishFlavor dishFlavor1 : flavors){
                dishFlavor1.setDishId(dishDTO.getId());
            }
            dishFlavorsMapper.insertBatch(flavors);
        }

    }

    @Override
    public Dish[] getByCategoryIdwhithFlavors(String categoryId) {
        Dish[] dishs = dishMapper.getDishByCategory(categoryId);
        return dishs;
    }

    /**
     * 修改菜品状态
     * @param status
     * @param id
     */
    @Override
    public void StartOrStop(Integer status, Long id) {
        Dish dish = new Dish();
        dish.setId(id);
        dish.setStatus(status);
        dishMapper.Update(dish);
    }

    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.list(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishMapper.getFlavorsById(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }
}
